//
//  DNMineVC.swift
//  LunalerApp
//
//  Created by 露乐ios on 2018/3/12.
//  Copyright © 2018年 露乐ios. All rights reserved.
//

import UIKit
import SolarNetwork
class DNMineVC: DNBaseViewController {

    @IBOutlet weak var tableView: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    static let kDNMineTableViewCellReUseID = "DNMineTableViewCell"
    
    let itemLists = NSMutableArray()
    var viewModel : DNMineProfileVM = {
        let vm = DNMineProfileVM().initWithDelegate(delegate:self as AnyObject)
        return vm
    }()
    
    
    override func initUI() {
        self.navigationController?.navigationBar.isHidden = true
        self.tableView.tableFooterView = UIView()
        self.tableView.separatorColor = rgb(r: 0xee, g: 0xee, b: 0xee)
        
        if #available(iOS 11.0, *) {
            self.tableView.contentInsetAdjustmentBehavior = .never
        }else{
            self.automaticallyAdjustsScrollViewInsets = false
        }
        
        self.tableView.estimatedRowHeight = 0
        self.tableView.estimatedSectionHeaderHeight = 0
        self.tableView.estimatedSectionFooterHeight = 0
        
        self.tableView.register(UINib.init(nibName: "DNMineTableViewCell", bundle: nil), forCellReuseIdentifier: DNMineVC.kDNMineTableViewCellReUseID)
        
        let itemArrPath = Bundle.main.path(forResource: "mineTabItem", ofType: ".plist")
        let itemArr = NSArray.init(contentsOfFile: itemArrPath!)
        self.itemLists.addObjects(from: itemArr as! [Any])

        
        self.viewModel.perform(self.viewModel.getUserProfileSEL, with: nil)
        
        NotificationCenter.default.addObserver(self, selector: #selector(DNMineVC.getCurrentUserProfileInfoAction(noti:)), name: NSNotification.Name(rawValue: kNOTIFICATION_MineTabSelect_NOTI()), object: nil)
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.init(kNOTIFICATION_MineTabSelect_NOTI()), object: nil)
    }
    
    @objc func getCurrentUserProfileInfoAction(noti:NSNotification) -> Void {
        debugPrint("getCurrentUserProfileInfoAction :\(noti)")
        self.viewModel.perform(self.viewModel.getUserProfileSEL, with: nil)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        self.navigationController?.setNavigationBarHidden(true, animated: true)
        let headView = self.tableView.tableHeaderView
        if headView != nil{
            //
            let headViewTemp = headView as! MineTableHeadView
            headViewTemp.refreshUI();
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}


extension DNMineVC : UITableViewDelegate,UITableViewDataSource{
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return self.itemLists.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let arr = self.itemLists.dnObjectAtIndex(idx: section) as! NSArray
        return arr.count
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView.init(frame: CGRect.init(x: 0, y: 0, width: kScreenWidth, height: 20))
        view.backgroundColor = rgb(r: 0xfa, g: 0xfa, b: 0xfa)
        return view
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return section == 0 ? 0 : 20;
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: DNMineVC.kDNMineTableViewCellReUseID) as! DNMineTableViewCell
        let arr = self.itemLists.dnObjectAtIndex(idx: indexPath.section) as! NSArray
        cell.itemConfigDict = arr.dnObjectAtIndex(idx: indexPath.row) as! NSDictionary
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 45.0
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        switch indexPath.section {
        case 3: do {
            let vc = DNSettingVC.defaultViewController()
            self.navigationController?.pushViewController(vc, animated: true)
            break
            }
        default:
            break
        }
    }
}

extension DNMineVC{
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        let headView = MineTableHeadView.mineTableHeadView()
        self.tableView.tableHeaderView = headView
        headView.refreshUI()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.tableView.reloadData()
    }
}



class DNMineProfileVM: DNBaseViewModel {
    
    private(set) var getUserProfileSEL : Selector? // 获取登录导购个人信息
    
    override func initWithDelegate(delegate: AnyObject) -> Self {
       super.initWithDelegate(delegate: delegate)
        getUserProfileSEL = #selector(getUserProfileAction(sender:))
       return self
    }
    
    @objc func getUserProfileAction(sender:AnyObject) -> Void{
        let api = DNApiForGetUserProfile.init()
        HTTPBinNetwork.request(api) { (response:SLResponse) in
            let model = DNDaoGouProfileBaseResponseModel.deserialize(from: response.dataDictionary)
//            DNUserContext.sharedInstance.ChannelName = model?.Data.
            debugPrint("DNApiForGetUserProfile response:\(model?.Message)====\(model?.Data)")
        }
    }

}






